package com.example.zhu95.mytimber.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.example.zhu95.mytimber.ConstantValue.albumArtBytesMap
import com.example.zhu95.mytimber.ConstantValue.emptyBlurBytes
import com.example.zhu95.mytimber.MyApplication
import com.example.zhu95.mytimber.R
import com.example.zhu95.mytimber.entity.SongBean
import java.io.ByteArrayOutputStream
import java.util.*

/**
 * @author zhu95
 * @date 2018/3/7 16:10
 */
object Util {
    private val foregroundColor = intArrayOf(
            R.color.blue_transparent,
            R.color.green_transparent,
            R.color.pink_transparent,
            R.color.purple_transparent,
            R.color.red_transparent)

    val randomForeColor: Int
        get() {
            val random = Random()
            return foregroundColor[random.nextInt(foregroundColor.size)]
        }
    val randomPositiveNum: Int
        get() {
            val random = Random()
            return Math.abs(random.nextInt())
        }

    /**
     * 将根据歌曲list获取歌曲的id字符串
     */
    fun getIdsStrFormat(songs: List<SongBean>): String {
        val sb = StringBuilder("");
        for (song in songs) {
            sb.append(song.id).append('$')
        }
        if (sb.toString() != "")
            sb.deleteCharAt(sb.lastIndex)
        return sb.toString()
    }

    fun getPlayTime(currentProgress: Int): String {
        val minute = currentProgress / 1000 / 60
        val second = (Math.round(currentProgress / 1000.toDouble())).toInt() % 60
        if (second.toString().length == 1) {
            return "$minute:0$second"
        }
        return "$minute:$second"
    }

    fun Bitmap2Bytes(bitmap: Bitmap): ByteArray {
        val baos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
        return baos.toByteArray()
    }

    fun getBlurBytes(resid: Int): ByteArray {
        if (emptyBlurBytes == null) {
            val bitmap = BitmapFactory.decodeResource(MyApplication.getContext().resources, resid)
            FastBlur.blurAsync(bitmap) {
                emptyBlurBytes = Bitmap2Bytes(it)
            }
        }
        return emptyBlurBytes
    }

    fun getBlurBytes(albumArt: String): ByteArray {
        var bytes = albumArtBytesMap[albumArt]
        if (bytes == null) {
            var byteArray = byteArrayOf()
            val bitmap = BitmapFactory.decodeFile(albumArt)
            FastBlur.blurAsync(bitmap) {
                byteArray = Bitmap2Bytes(it)
            }
            bytes = byteArray;
            albumArtBytesMap.put(albumArt, bytes)
        }
        return bytes
    }

    /**
     * list的equals方法
     */
    fun listEquals(list1: List<Any>, list2: List<Any>): Boolean {
        if (list1.size != list2.size) {
            return false
        }
        for (i in list1.indices) {
            if (list1[i] != list2[i]) {
                return false
            }
        }
        return true
    }

    /**
     * 将歌曲id从歌曲id字符串中移除
     */
    fun removeSongidFromSongids(songid: Int, songids: String): String {
        val toString = songid.toString()
        if (toString == songids) {
            return ""
        }
        var replace = "";
        if (songids.contains(toString)) {
            if (songids.endsWith(toString)) {
                replace = songids.replace("$" + toString, "")
            } else {
                replace = songids.replace(toString + "$", "")
            }
        }
        return replace
    }
}
